package com.tianshi.yingxue.service.impl;

import com.tianshi.yingxue.dao.AdminDao;
import com.tianshi.yingxue.entity.Admin;
import com.tianshi.yingxue.service.AdminService;
import com.tianshi.yingxue.utils.ImageCodeUtil;
import com.tianshi.yingxue.utils.Md5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Service
@Transactional
public class AdminServiceImpl implements AdminService {
    @Autowired
    private AdminDao adminDao;
    @Override
    public void register(Admin admin){
        String salt = Md5Utils.getSalt(6);
        admin.setSalt(salt);
        String password = salt+admin.getPassword()+salt;
        String md5Code = Md5Utils.getMd5Code(password);
        admin.setPassword(md5Code);
        adminDao.insertAdmin(admin);
    }
    @Autowired
    private RedisTemplate<String,Object> redisTemplate;
    @Override
    public Map<String,String> getImageCodes() throws IOException{
        String securityCode = ImageCodeUtil.getSecurityCode(4,ImageCodeUtil.SecurityCodeLevel.Medium,false);
        String imgBase64 = ImageCodeUtil.careateImgBase64(securityCode);
        String token= UUID.randomUUID().toString();
        redisTemplate.opsForValue().set("CODE_" + token,securityCode,300, TimeUnit.SECONDS);
        redisTemplate.opsForValue().set("TOKEN_" + token,token,300, TimeUnit.SECONDS);
        Map<String ,String> result = new HashMap<>();
        result.put("imgCode",imgBase64);
        result.put("token",token);
        return result;
    }
    @Override
    public Map<String, String> login(Admin admin,String token){
        Map<String,String> map = new HashMap<>();
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        Object obj = redisTemplate.opsForValue().get("CODE_" + token);
        if (obj == null){
            map.put("message","验证码过期");
            map.put("state","error");
            return map;
        }
        String serverCode = obj.toString();
        if (!admin.getEnCode().equals(serverCode)){
            map.put("message","验证码登录");
            map.put("state","error");
            return map;
        }
        Admin user = adminDao.selectByUsername(admin.getUsername());
        if (user == null){
            map.put("message","账号不存在");
            map.put("state","error");
            return map;
        }
        if (user.getStatus() == "0"){
            map.put("message","用户已被冻结");
            map.put("state","error");
            return map;
        }
        String inputPwdWithSalt = user.getSalt()+admin.getPassword()+user.getSalt();
        String md5Pwd = Md5Utils.getMd5Code(inputPwdWithSalt);
        if (md5Pwd.equals(user.getPassword())){
            map.put("state","success");
            map.put("message",token);
            redisTemplate.opsForValue().set("TOKEN_"+token,user);
        } else {
            map.put("message","密码错误");
            map.put("state","error");
        }
        return map;
    }
    @Override
    public Admin getUserInfo(String token){
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        return (Admin) redisTemplate.opsForValue().get("TOKEN_"+token);
    }
    @Override
    public void logout(String token){
        redisTemplate.delete("TOKEN_"+token);
        redisTemplate.delete("CODE_"+token);
    }
}
